//
//  SearchController.m
//  QQplayground
//
//  Created by zhangxy on 2018/6/29.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "SearchController.h"
#import "ZZRSTableController.h"
#import "UIImage+ImageWithColor.h"
#import "SVWebViewController.h"
#import "ZZSearchResultCell.h"
#import "ZZHomeTopMenu.h"

@interface SearchController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate,UISearchResultsUpdating>{
    UISearchController * _searchVC;//搜索视图
    ZZRSTableController * _searchTableView;//搜索结果的表格视图
    
    int page;
}


@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray * listArray;

@end

@implementation SearchController

- (void)loadCollectionView
{
    _collectionView = ({
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake((ScreenWidth-10)/2, 165);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = 5;
        
        // 12的间隙为 item 到 消息
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight+44, ScreenWidth,ScreenHeight-NavBarHeight-44) collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor clearColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerClass:[ZZSearchResultCell class] forCellWithReuseIdentifier:kZCCollectionViewCellID];
        [self.view addSubview:collectionView];
        collectionView;
    });
    
    
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    [self.navigationController setNavigationBarHidden:YES];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}
-(void)setNavigationBar{
    self.navigationItem.leftBarButtonItem = nil;
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationBar];
    
    _listArray = [[NSMutableArray alloc] init];
    page = 1;
    
    [self loadCollectionView];
    [self createSearchController];
    if(convertToString(_searchText).length>0){
        [_searchVC.searchBar setText:_searchText];
    }else{
        [self allAppRequestWithParm];
    }
}

#pragma mark 搜索视图
- (void)createSearchController
{
    //表格界面 UITableViewController表格视图控制器 tableview是表格视图
    _searchTableView = [[ZZRSTableController alloc] init];
    //创建搜索界面
    _searchVC = [[UISearchController alloc]initWithSearchResultsController:_searchTableView];
//        _searchVC.delegate=self;
    //把表格视图控制器跟搜索界面相关联（防止searchBar发生64像素的偏移量）
    self.definesPresentationContext = YES;
    __block SearchController *safeSelf = self;
    [_searchTableView setBlock:^(NSString *link) {
//        NSString *webUrl = urlEncodedString(link);
//        NSURL *url = [NSURL URLWithString:webUrl];
        
//        SVWebViewController *web = [[SVWebViewController alloc] initWithURL:url];
//        [safeSelf openNav:web sound:nil];
    }];
    
    //在tableView存在右侧索引情况下防止搜索框右侧缺少一块
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, ScreenWidth, NavBarHeight + 44);
    [headerView setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, StatusBarHeight-5, ScreenWidth, 44)];
    [searchView addSubview:_searchVC.searchBar];
    
    [headerView addSubview:searchView];
    _searchVC.searchBar.showsCancelButton = YES;
    _searchVC.searchResultsUpdater = self;
//    _searchVC.hidesNavigationBarDuringPresentation = YES;
    //修改searchBar的属性
//    if(!ZC_iPhoneX){
        UIView *subView =_searchVC.searchBar.subviews[0];
        for (UIView *view in subView.subviews)
        {
            if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [view removeFromSuperview];
                // 修改搜索框背景
                UIView *backView = [[UIView alloc] init];
                backView.backgroundColor = UIColorFromRGB(BgTitleColor);
                backView.frame = CGRectMake(0, -20, ScreenWidth, 64);
                backView.layer.cornerRadius = 2.0f;
                backView.layer.masksToBounds = YES;
                [_searchVC.searchBar insertSubview:backView atIndex:0];
            }
            //自定义textField
            else if ([view isKindOfClass:[UITextField class]])
            {
                UITextField *textField = (UITextField *)view;
//                textField.layer.borderWidth = 0.5;
//                textField.layer.borderColor = UIColorFromRGB(BgLineColor).CGColor;
//                textField.layer.cornerRadius = 2;
//                textField.layer.masksToBounds = YES;
//                textField.clipsToBounds = YES;
            }else if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
                UIButton * cancel =(UIButton *)view;
                [cancel setTitleColor:UIColor.whiteColor forState:0];
                [cancel setTitle:@"取消" forState:UIControlStateNormal];
            }
        }
//    }
    [self.view addSubview:headerView];
    
    ZZHomeTopMenu *menu = [[ZZHomeTopMenu alloc] initWithFrame:CGRectMake(20, NavBarHeight, ScreenWidth-40,44) array:@[@"素材",@"帖子",@"用户"] textColor:UIColorFromRGB(TextWhiteColor) checkColor:UIColorFromRGB(TextWhiteColor) result:^(NSInteger tag, NSString *title) {
        
//        [self.listTable.header beginRefreshing];
    }];
    [menu setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:menu];
}

#pragma mark 搜索的协议方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    //这句代码实现cell顶部和searchBar不发生重叠
    self.edgesForExtendedLayout = UIRectEdgeAll;
    //在点击搜索时会调用一次，点击取消按钮又调用一次
    //判断当前搜索是否在搜索状态还是取消状态
    if (_searchVC.isActive) {
        if (searchController.searchBar.text.length!=0) {
            
            //表示搜索状态
            //调取搜索的接口
            
            [self searchAllDateRequestWithDict:searchController.searchBar.text];
        }
    }
}
#pragma mark -- UISearchControllerDelegate
- (void)willPresentSearchController:(UISearchController *)searchController{
    [UIView animateWithDuration:0.1 animations:^{
        //修改取消按钮字体颜色
        searchController.searchBar.showsCancelButton = YES;
        UIButton *cancelBtn=[searchController.searchBar valueForKey:@"cancelButton"];
        [cancelBtn setTitleColor:UIColorFromRGB(BgTitleColor) forState:UIControlStateNormal];
    }];
}


#pragma mark -- 请求数据
- (void)allAppRequestWithParm
{
    //调取接口
    //刷新数据
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:convertToString(_searchText) forKey:@"title"];
    [dict setObject:@"24" forKey:@"pageSize"];
    [dict setObject:convertIntToString(page) forKey:@"pageNum"];
    
}

#pragma mark -- 通讯录搜索接口
- (void)searchAllDateRequestWithDict:(NSString *)text
{
    //调取接口
    //刷新搜索界面的tableview
    [_searchTableView loadResult:text];
}

////////////////////////


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _listArray.count;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    return [collectionView dequeueReusableCellWithReuseIdentifier:kZCCollectionViewCellID forIndexPath:indexPath];
}

// 点击发送消息
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 发送点击消息
    
//    LemmaModel * itemModel = _listArray[indexPath.row];
//    NSString *webUrl = urlEncodedString(itemModel.lemmaUrl);
//    NSURL *url = [NSURL URLWithString:webUrl];
    
//    SVWebViewController *web = [[SVWebViewController alloc] initWithURL:url];
//    [self openNav:web sound:nil];
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    LemmaModel * model = _listArray[indexPath.row];
//
//    [(ZZSearchResultCell *)cell configureCellWithPostURL:model];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
